var logger = require('log4js').getLogger();
var request = require('request');
var config = require('../../../config.json');
var sdp_server = config.sdp_server.host + ":" + config.sdp_server.port;

exports.addWkpMember = function (req, res, next) {
    var workspace_id = req.params.workspace_id;
    var newMember = req.body;
    request.post(sdp_server + "/api/workspace/member/"+ workspace_id,
        {form: newMember},
        function (err, response, data) {
            var dataJson = JSON.parse(data);
            if (err) {
                logger.error("添加成员失败");
                logger.error(err);
                return res.format_json(null, 500, "添加成员失败!");
            } else {
                res.format_json(dataJson, dataJson.code,dataJson.message);
            }
        });
},
exports.removeWkpMember = function (req, res, next) {
    var workspace_id = req.params.workspace_id;
    var shortId = req.query.user;
    request({
        url: sdp_server + "/api/workspace/member/" + workspace_id + "?user=" + shortId,
        method: "DELETE",
        json: true
    }, function (err, response, data) {
            if (err) {
                logger.error("删除成员失败");
                logger.error(err);
                return res.format_json(null, 500, "删除成员失败!");
            } else {
                res.format_json(data, data.code);
            }
        });
},
exports.editWkpMember = function (req, res, next) {
    var workspace_id = req.params.workspace_id;
    var member = req.body;
    request({
        url: sdp_server + "/api/workspace/member/" + workspace_id,
        method: "PUT",
        json: true,
        body:member
    }, function (err, response, data) {
        if (err) {
            logger.error("删除成员失败");
            logger.error(err);
            return res.format_json(null, 500, "删除成员失败!");
        } else {
            res.format_json(data, data.code);
        }
    });
};